/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;
import org.apache.log4j.Logger;
import persistence.constant.Log4jPropertiesLoader;

/**
 *
 * @author Ricardo
 */
public class FileManager {

    private static org.apache.log4j.Logger log = Logger.getLogger(Log4jPropertiesLoader.class);

    public void writeFile(String folderPath, String fileName, String data) {

        try {
            
            this.createFolder(folderPath);
            File file = new File(folderPath+"\\"+fileName);
            //if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fileWritter = new FileWriter(file);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(data);
            bufferWritter.flush();
            bufferWritter.close();
            log.info("Write file Done");

        } catch (IOException e) {
            log.error("Error in write file", e);
        }
    }
    
    /** Read the contents of the given file. */
  public String readFile( String fileName ) {
    log.info("Reading from file.");
    StringBuilder text = new StringBuilder();
    String NL = System.getProperty("line.separator");
    Scanner scanner = null;
    try {
      scanner = new Scanner(new FileInputStream(fileName), "utf-8");
      while (scanner.hasNextLine()){
        text.append(scanner.nextLine() + NL);
      }
    }
    catch( Exception e ){
        log.error("Error in readFile", e);
    }
    finally{
      scanner.close();
    }
    log.info("Text read in: " + text);
    return text.toString();
    
  }
    
    private void createFolder(String folderPath) {
        File folder = new File(folderPath);
        boolean result = false;
        if (!folder.exists()) {
            log.info("creating directory:" + folderPath);
            result = folder.mkdirs();
        }
        if (result) {
            log.info("directory created:" + folderPath);
        }
    }
    
    public Collection<String> getFileList(String folderPath) {
        Collection<String> fileListColl = new ArrayList();
        File folder = new File(folderPath);
        String[] fileList = folder.list();
        if (fileList == null) {
            System.out.println("There is not file Path:" + folderPath);
        } else {
            for( int i = 0; i< fileList.length; i++){
                fileListColl.add(folderPath+"\\"+fileList[i]);
            }
        }
        
        return fileListColl;
    }
    
    public String getFileName(String path) {
        String fileName = path.substring(path.lastIndexOf("\\") + 1, path.length());
        return fileName;
    }
}
